package com.qfedu.web;

import com.qfedu.pojo.Account;
import com.qfedu.pojo.Cards;
import com.qfedu.pojo.User;
import com.qfedu.service.AccountService;
import com.qfedu.service.CardsService;
import com.qfedu.service.HistoryService;
import com.qfedu.service.impl.AccountServiceImpl;
import com.qfedu.service.impl.CardsServiceImpl;
import com.qfedu.service.impl.HistoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/transferServlet")
public class TransferServlet extends BaseServlet {
    AccountService accountService = new AccountServiceImpl();
    Account account = accountService.findMoney();

    //查询余额，并提示到转账界面
    protected void list1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先判断是否登录
        User user = (User) req.getSession().getAttribute("user");
        if (user != null) {
            resp.sendRedirect("/page/transfer_accounts.jsp");
        } else {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
        }
        BigDecimal money = account.getMoney();
        String m1 = money.toString();
        HttpSession session = req.getSession();
        session.setAttribute("money", m1);
        req.getRequestDispatcher("/page/transfer_accounts.jsp").forward(req, resp);
    }

    //判断输入的信息
    protected void list2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //取输入框中的验证码
        String verifycode = req.getParameter("verifycode");
        //去随机生成的
        String check_code = (String) req.getSession().getAttribute("CHECK_CODE");
        if (!check_code.equalsIgnoreCase(verifycode)) {
            req.getSession().setAttribute("msg", "验证码输入错误");
            req.getRequestDispatcher("/page/transfer_accounts.jsp").forward(req, resp);
        }
        AccountService accountService = new AccountServiceImpl();
        String name = req.getParameter("name");
        String cardnumber = req.getParameter("cardnumber");
        String bank_name = req.getParameter("bank_name");
        String idcode = req.getParameter("idcode");
        String m = req.getParameter("money");
        String password = req.getParameter("password");
        String pwd = req.getParameter("pwd");
        Account account2 = new Account();
        account2.setIdcode(idcode);
        account2.setPaypassword(password);
        Account a = accountService.find1(account2);

        Cards cards = new Cards();
        cards.setName(name);
        cards.setCardnumber(cardnumber);
        cards.setBank_name(bank_name);
        CardsService cardsService = new CardsServiceImpl();
        Cards c = cardsService.find2(cards);


        if (a == null || c == null) {
//            resp.getWriter().write("转账失败，信息填写错误");

            //加
            req.getSession().setAttribute("m", "转账失败，信息填写错误");
            req.getRequestDispatcher("/page/transfer_accounts.jsp").forward(req, resp);
        } else {
            if (!password.equals(pwd)) {
                resp.getWriter().write("两次密码输入不一致");
            }
            Account source = accountService.findAccountIdcode(idcode);
            Cards target = cardsService.findCardsnumber(cardnumber);

            //把转账记录添加到历史记录
            String transferee = source.getIdcode() + target.getCardnumber();
            String outtransferee = target.getCardnumber();
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.ddHH:mm:ss");
            String time = sdf.format(date);
            BigDecimal m2 = new BigDecimal(m);
            HistoryService historyService = new HistoryServiceImpl();
            historyService.addHistory(source.getIdcode(), transferee, outtransferee, m2, time, 2);

            accountService.transfer(source, target, Integer.parseInt(m));
            //resp.getWriter().write("转账成功");

            BigDecimal money = account.getMoney();
            String m1 = money.toString();
            req.getSession().setAttribute("zhuan", "转账成功！");
            req.getSession().setAttribute("zhang", m1);
            resp.sendRedirect("/page/transfer_success.jsp");


        }
    }
}
